package edu.zut.cs.software.example.Space.web.spring.controller;

import java.util.HashMap;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;

import edu.zut.cs.software.example.QueryInfo.QueryInfo;
import edu.zut.cs.software.example.space.dao.SpaceDao;
import edu.zut.cs.software.example.space.domain.Space;
import edu.zut.cs.software.example.space.service.SpaceManager;

/**
 * 
 * @author ma jiang tao
 *
 */

@RestController
public class SpaceController {

	@Autowired
	SpaceManager spaceManager;

	@Autowired
	SpaceDao spaceDao;
	/**
	 * 默认查询该用户的所有学习笔记
	 * 
	 * @param queryInfo
	 * @return
	 */
	@RequestMapping("/space")
	public String getSpaceList(QueryInfo queryInfo) {
		//System.out.println("前端携带数据： "+queryInfo+" 请求/getById");
		int numbers = spaceManager.getSpaceByStudentIdCounts(queryInfo.getId());
		//System.out.println("numbers:"+numbers);
		int pagenum = (queryInfo.getPageNum() - 1) * queryInfo.getPageSize();
		//System.out.println("pageStart:"+pagenum);

		List<Space> Space = spaceManager.findByStudentId(queryInfo.getId(), pagenum, queryInfo.getPageSize());

		//System.out.println("查询结果列表："+Space);
		HashMap<String, Object> res = new HashMap<>();
		res.put("numbers", numbers);
		res.put("data", Space);

		String res_string = JSON.toJSONString(res);

		return res_string;

	}

	/**
	 * 添加学习笔记
	 * 
	 * @param space
	 * @return
	 */
	@RequestMapping("/addspace")
	public String addspace(@RequestBody Space space) {// 传来的数据转换为对象json->obj
		Space addspa = spaceManager.save(space);
		return null;
	}

	/**
	 * 删除学习笔记
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping("/deletespace")
	public String deletespace(int id) {
		//System.out.println("*****ID******:" + id);
		int i = spaceManager.deleteById(id);
		//System.out.println("*****result******:" + i);
		String str = i >= 0 ? "success" : "error";
		//System.out.println("*****result******:" + str);
		return str;
	}

	/**
	 * 修改学习笔记
	 * 
	 * @param space
	 * @return
	 */

	@RequestMapping("/editspace")
	public String editspace(@RequestBody Space space) {
		String flag = "error";

		//System.out.println(space);
		
		int spa = spaceManager.editspace(space);
		
		// System.out.println(spa);
		 
		if (spa == 1)
			flag = "success";
		HashMap<String, Object> res = new HashMap<String, Object>();
		res.put("flag", flag);

		int id = Integer.parseInt(String.valueOf(space.getId()));

		Space newSpaInfo = spaceManager.getUpdateSpaService(id);

		res.put("space", newSpaInfo);

		String r = JSON.toJSONString(res);

		return r;
	}

	/**
	 * 按标题查找
	 * 
	 * @param queryInfo
	 * @return
	 */
	@RequestMapping("/getByname")
	public String getSpaceListByName(QueryInfo queryInfo) {
//			System.out.println("前端携带数据： "+queryInfo+" 请求/getByState");
		int numbers = spaceManager.getSpaceBymessageCounts(queryInfo.getQuery());
//			System.out.println("numbers:"+numbers);

		int pageStart = (queryInfo.getPageNum() - 1) * queryInfo.getPageSize();
//			System.out.println("pageStart:"+pageStart);

		List<Space> Space = spaceManager.findBymessage(queryInfo.getQuery(), pageStart, queryInfo.getPageSize());
		//System.out.println("查询结果列表：" + Space);

		HashMap<String, Object> res = new HashMap<>();
		res.put("numbers", numbers);
		res.put("data", Space);

		String res_string = JSON.toJSONString(res);

		return res_string;

	}

	/**
	 * 查找修改的数据
	 * 
	 * @param space
	 * @return
	 */
	@RequestMapping("/getupdatespace")
	public String getUpdateSpace(int id) {
		// System.out.println(id);
		String spa = JSON.toJSONString(spaceManager.getUpdateSpaService(id));
		// System.out.println(spa);
		return spa;
	}
}
